package com.bo.controller;

import com.bo.pojo.Emp;
import com.bo.pojo.Result;
import com.bo.service.EmpService;
import com.bo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author huangbo
 * @version 1.0.0
 */
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("登录,用户名{} ，密码{}", emp.getUsername(), emp.getPassword());
        Emp emp1 = empService.login(emp);

        //登录成功：生成令牌并且下发令牌
        if (emp1 != null) {
            HashMap<String, Object> cliams = new HashMap<>();
            cliams.put("id", emp1.getId());
            cliams.put("name", emp1.getName());
            cliams.put("username", emp1.getUsername());

            String jwt = JwtUtils.generateJwt(cliams);
            return Result.success(jwt);
        }

        //登录失败：返回错误信息
        else {
            return Result.error("errorPassword");
        }
    }


}
